package zhongbo.java.air.airmonitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.airmonitor.mapper.AirAlertLogMapper;
import zhongbo.java.air.airmonitor.model.AirAlertLog;
import zhongbo.java.air.airmonitor.service.IAirAlertLogService;
import zhongbo.java.air.common.vo.PageVo;

import java.util.Date;

@Service
public class AirAlertLogServiceImpl implements IAirAlertLogService<AirAlertLog> {

    @Autowired
    private AirAlertLogMapper airAlertLogMapper;

    @Override
    public AirAlertLog insert(AirAlertLog airAlertLog) {
        int insert = airAlertLogMapper.insert(airAlertLog);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + airAlertLog);
        }
        return airAlertLog;
    }

    @Override
    public AirAlertLog updateById(AirAlertLog airAlertLog) {
        int i = airAlertLogMapper.updateById(airAlertLog);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> " + airAlertLog);
        }
        return airAlertLog;
    }

    @Override
    public AirAlertLog queryById(String id) {
        AirAlertLog airAlertLog = airAlertLogMapper.selectById(id);
        if (airAlertLog == null){
            throw new RuntimeException("查询失败 ->>> id：" + id);
        }
        return airAlertLog;
    }

    @Override
    public String deleteById(String id) {
        int i = airAlertLogMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
        return "删除成功";
    }

    @Override
    public PageVo<AirAlertLog> queryByPage(Integer pageNum, Integer pageSize, String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper();
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }
        IPage<AirAlertLog> pageParam = new Page<>(pageNum,pageSize);
        IPage<AirAlertLog> airAlertLogIPage = airAlertLogMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airAlertLogIPage);
    }

    @Override
    public AirAlertLog queryByIcaoCodeAndTriggerTime(String icaoCode, Date triggerTime) throws RuntimeException{
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("icao_code",icaoCode);
        // 查询5分钟内 triggerTime
        queryWrapper.between("trigger_time",new Date(triggerTime.getTime() - 5 * 60 * 1000),triggerTime);
        try {
            AirAlertLog airAlertLog = airAlertLogMapper.selectOne(queryWrapper);
            return airAlertLog;
        }catch (RuntimeException e){
            throw new RuntimeException("查询失败 ->>> icaoCode：" + icaoCode + " triggerTime：" + triggerTime);
        }
    }
}
